package com.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Map;

@RestController
public class TestPost {
    @PostMapping("/download")
    @CrossOrigin
    public void downloadFile(@RequestParam Map<String,Object> param, HttpServletResponse response) throws Exception {
        System.out.println("收到消息");
        // 读取文件内容
        File file = new File("C:\\Users\\magicXd\\Desktop\\工作\\路线.txt");
        FileInputStream inputStream = new FileInputStream(file);

        // 设置响应头
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

        // 写入文件内容到输出流
        OutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        Thread.sleep(10000);

        inputStream.close();
        outputStream.close();
    }

    @PostMapping("/test")
    public String testPost( HttpServletResponse response) throws Exception {
        // 设置允许跨域的域名，这里允许所有域名
        response.setHeader("Access-Control-Allow-Origin", "*");
        // 设置允许的请求方法
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
        // 设置允许的请求头
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
        // 允许发送 cookies
        response.setHeader("Access-Control-Allow-Credentials", "true");




       return "hello Post";
    }
    @GetMapping("/testG")
    public String testG() throws Exception {

        return "hello G";
    }

}
